package com.tfswue.hql.controller;

import com.tfswue.hql.common.Result;
import com.tfswue.hql.entity.Order;
import com.tfswue.hql.exception.BusinessException;
import com.tfswue.hql.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    @GetMapping("/{id}")
    public Result<Order> getOrderById(@PathVariable Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(400, "订单ID不能为空或小于等于0");
        }
        Order order = orderService.getOrderById(id);
        if (order == null) {
            return Result.notFound("订单不存在");
        }
        return Result.success(order);
    }
    
    @GetMapping
    public Result<List<Order>> getAllOrders() {
        List<Order> orders = orderService.getAllOrders();
        return Result.success(orders);
    }
    
    @GetMapping("/user/{userId}")
    public Result<List<Order>> getOrdersByUserId(@PathVariable Long userId) {
        if (userId == null || userId <= 0) {
            throw new BusinessException(400, "用户ID不能为空或小于等于0");
        }
        List<Order> orders = orderService.getOrdersByUserId(userId);
        return Result.success(orders);
    }
    
    @PostMapping
    public Result<Order> createOrder(@RequestBody Order order) {
        if (order == null) {
            throw new BusinessException(400, "订单信息不能为空");
        }
        if (order.getName() == null || order.getName().trim().isEmpty()) {
            throw new BusinessException(400, "订单名称不能为空");
        }
        if (order.getPrice() == null || order.getPrice() <= 0) {
            throw new BusinessException(400, "订单价格必须大于0");
        }
        if (order.getUserId() == null || order.getUserId() <= 0) {
            throw new BusinessException(400, "用户ID不能为空或小于等于0");
        }
        Order createdOrder = orderService.createOrder(order);
        return Result.success("订单创建成功", createdOrder);
    }
    
    @PutMapping("/{id}")
    public Result<Order> updateOrder(@PathVariable Long id, @RequestBody Order order) {
        if (id == null || id <= 0) {
            throw new BusinessException(400, "订单ID不能为空或小于等于0");
        }
        if (order == null) {
            throw new BusinessException(400, "订单信息不能为空");
        }
        if (order.getName() == null || order.getName().trim().isEmpty()) {
            throw new BusinessException(400, "订单名称不能为空");
        }
        if (order.getPrice() == null || order.getPrice() <= 0) {
            throw new BusinessException(400, "订单价格必须大于0");
        }
        order.setId(id);
        Order updatedOrder = orderService.updateOrder(order);
        if (updatedOrder == null) {
            return Result.notFound("订单不存在");
        }
        return Result.success("订单更新成功", updatedOrder);
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteOrder(@PathVariable Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(400, "订单ID不能为空或小于等于0");
        }
        boolean deleted = orderService.deleteOrder(id);
        if (!deleted) {
            return Result.notFound("订单不存在");
        }
<<<<<<< HEAD
        return Result.success();
=======
        return Result.success("订单删除成功");
>>>>>>> a7b1b27c1ad19c478259be0487c89c893b108f50
    }
}
